package com.edu.dreams.controller;

import com.edu.dreams.entity.News;
import com.edu.dreams.mapper.NewsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

//@Controller注解
@Controller

public class AdminController {
	
	@Autowired
	NewsMapper newsMapper;

// 向left页面跳转
	@RequestMapping("left")
	public String left() {
		return "left";
	}

//向查看资讯页面跳转
	//@RequestParam：将请求参数绑定到你控制器的方法参数上（是springmvc中接收普通参数的注解）
	@RequestMapping("seenew")
	public String seenew(@RequestParam(required = false, defaultValue = "1") int startPage,
			@RequestParam(required = false, defaultValue = "6") int pagesize, Model model) {
		PageHelper.startPage(startPage, pagesize);
		PageInfo<News> newsdata = new PageInfo<>(newsMapper.getAllNews());
		model.addAttribute("pageNumNow", startPage);
		model.addAttribute("news", newsdata);
		return "seenew";
	}

	// 向添加资讯页面跳转
	@RequestMapping("addnews")
	public String addNews() {
		return "addnews";
	}

//向更新资讯页面跳转
	@RequestMapping("updateNewsPage")
	public String updateNewsPage(int id, Model model) {
		model.addAttribute("info", newsMapper.getNewsById(id));
		return "update_news";
	}
}
